Attribute VB_Name = "Import_text_data_prompt"
Option Explicit

' **********************
' DEFAULTs and CONSTANTS
' **********************

Private Const DEF_TILESPACE = 20#
Private Const TEXTFONT = "Arial"
Private Const TEXTSPACING = 3#
Private Const LEGENDWIDTH = 20#
Private Const LEGENDHEIGHT = 10#
Private Const THUMBNAILHEIGHT = 20#
Private Const THUMBNAILWIDTH = 20#

' ********************
' OPEN DIALOG
' ********************


'API declarations
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OpenFilename) As Long
Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'data buffer for the GetOpenFileName and GetSaveFileName functions
Private Type OpenFilename
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        iFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
End Type

'Open file dialog flag
Private Const OFN_HIDEREADONLY = &H4

'as VBA has no Hwnd(window handle) property, this function will find the Hwnd
Private Function getHwnd()
getHwnd = FindWindow("ThunderDFrame", "Common Open Dialog Example")
End Function






Private Function SelectFile() As String

    'Ask user for the palette file
    Dim FileName As String
    Dim ofn As OpenFilename
    Dim strFilter As String
    Dim lngApiReturn As Long
    
    'set up filter
    strFilter = "All Files (*.*)" & Chr(0) & "*.*"
    
    'initialize the ofn structure
    ofn.lStructSize = Len(ofn)
    ofn.flags = OFN_HIDEREADONLY
    ofn.hwndOwner = 0
    ofn.lpstrFilter = strFilter
    ofn.iFilterIndex = 1
    ofn.lpstrFile = String(257, 0)
    ofn.nMaxFile = Len(ofn.lpstrFile) - 1
    ofn.lpstrFileTitle = ofn.lpstrFile
    ofn.nMaxFileTitle = ofn.nMaxFile
    ofn.lpstrTitle = "Open Palette File"
    ofn.lpstrInitialDir = CurDir
    
    'call the API function
    lngApiReturn = GetOpenFileName(ofn)

    If lngApiReturn <> 0 Then
        SelectFile = ofn.lpstrFile
    Else
        End
    End If
    Exit Function
ErrorOpenFile:
    MsgBox "Unable to open " & FileName
    End
End Function

Public Sub import_text_data_prompt()
    Dim s As Shape
    Dim x1 As Double, y1 As Double
    Dim printtxt As String
    
    Dim FileName As String
    FileName = SelectFile
    
    Dim crv As Curve
    Set crv = CreateCurve(ActiveDocument)
    
    Dim sr As New ShapeRange
    
    Dim ap As Page
    Dim scale_x As Double
    Dim scale_y As Double
    ActivePage.GetSize scale_x, scale_y
    
    Dim font_size As Double
    font_size = CDbl(InputBox("Enter font size (in mm):"))
    
    Dim fs, f, ts, str1, str2, str, pos
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(FileName)
    Set ts = f.OpenAsTextStream(1, -2)
    
    Dim x As Double, y As Double, w As Double, h As Double
    Do While ts.atendofstream <> True
            str = ts.ReadLine
            pos = InStr(1, str, Chr(9))
            x1 = Val(Left(str, pos)) * scale_x
            
            str = Mid(str, pos + 1)
            pos = InStr(1, str, Chr(9))
            y1 = Val(Left(str, pos)) * scale_y
            
            printtxt = Mid(str, pos + 1)
            Set s = ActiveLayer.CreateArtisticText(x1, y1, printtxt, cdrLanguageNone, _
                                                    cdrCharSetMixed, "Times New Roman", font_size, cdrUndefined, _
                                                    cdrUndefined, cdrMixedFontLine, cdrCenterAlignment)
            s.GetBoundingBox x, y, w, h
            x = x + w / 2
            y = y + h / 2
            s.Move x1 - x, y1 - y
            sr.Add s
    Loop
    ts.Close
    
    sr.AddToSelection
    Application.ActiveSelection.group
    
    
    

End Sub

Public Sub test_choose_font()
d_choose_font.Show
End Sub
